home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtox10.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  2.8 KB  |  121 lines

  1. /* pbmtox10bm.c - read a portable bitmap and produce an X10 bitmap file
  2. **
  3. ** Copyright (C) 1988 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14.  
  15. void
  16. main( argc, argv )
  17.     int argc;
  18.     char* argv[];
  19.     {
  20.     FILE* ifp;
  21.     bit* bitrow;
  22.     register bit* bP;
  23.     int rows, cols, format, padright, row;
  24.     register int col;
  25.     char name[100];
  26.     char* cp;
  27.     int itemsperline;
  28.     register int bitsperitem;
  29.     register int item;
  30.     int firstitem;
  31.     char* hexchar = "0123456789abcdef";
  32.  
  33.     pbm_init( &argc, argv );
  34.  
  35.     if ( argc > 2 )
  36.     pm_usage( "[pbmfile]" );
  37.  
  38.     if ( argc == 2 )
  39.     {
  40.     ifp = pm_openr( argv[1] );
  41.     strcpy( name, argv[1] );
  42.     if ( strcmp( name, "-" ) == 0 )
  43.         strcpy( name, "noname" );
  44.  
  45.     if ( ( cp = index( name, '.' ) ) != 0 )
  46.         *cp = '\0';
  47.     }
  48.     else
  49.     {
  50.     ifp = stdin;
  51.     strcpy( name, "noname" );
  52.     }
  53.  
  54.     pbm_readpbminit( ifp, &cols, &rows, &format );
  55.     bitrow = pbm_allocrow( cols );
  56.  
  57.     /* Compute padding to round cols up to the nearest multiple of 16. */
  58.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  59.  
  60.     fprintf (stdout,  "#define %s_width %d\n", name, cols );
  61.     fprintf (stdout,  "#define %s_height %d\n", name, rows );
  62.     fprintf (stdout,  "static short %s_bits[] = {\n", name );
  63.  
  64.     itemsperline = 0;
  65.     bitsperitem = 0;
  66.     item = 0;
  67.     firstitem = 1;
  68.  
  69. #define PUTITEM \
  70.     { \
  71.     if ( firstitem ) \
  72.     firstitem = 0; \
  73.     else \
  74.     putchar( ',' ); \
  75.     if ( itemsperline == 11 ) \
  76.     { \
  77.     putchar( '\n' ); \
  78.     itemsperline = 0; \
  79.     } \
  80.     if ( itemsperline == 0 ) \
  81.     putchar( ' ' ); \
  82.     ++itemsperline; \
  83.     putchar('0'); \
  84.     putchar('x'); \
  85.     putchar(hexchar[item >> 12]); \
  86.     putchar(hexchar[(item >> 8) & 15]); \
  87.     putchar(hexchar[(item >> 4) & 15]); \
  88.     putchar(hexchar[item & 15]); \
  89.     bitsperitem = 0; \
  90.     item = 0; \
  91.     }
  92.  
  93. #define PUTBIT(b) \
  94.     { \
  95.     if ( bitsperitem == 16 ) \
  96.     PUTITEM; \
  97.     if ( (b) == PBM_BLACK ) \
  98.     item += 1 << bitsperitem; \
  99.     ++bitsperitem; \
  100.     }
  101.  
  102.     for ( row = 0; row < rows; ++row )
  103.     {
  104.     pbm_readpbmrow( ifp, bitrow, cols, format );
  105.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  106.         PUTBIT(*bP);
  107.     for ( col = 0; col < padright; ++col )
  108.         PUTBIT(0);
  109.         }
  110.  
  111.     pm_close( ifp );
  112.     
  113.     if ( bitsperitem > 0 )
  114.     PUTITEM;
  115.     fprintf (stdout,  "};\n" );
  116.  
  117.     pm_close (stdout);
  118.  
  119.     exit( 0 );
  120.     }
  121.